//
//  EeImagesCellSupport.swift
//  SmoSky
//
//  Created by zhu xietong on 2018/5/30.
//  Copyright © 2018年 tianrun. All rights reserved.
//

import Foundation
import Eelay
import AsyncDisplayKit
import JXPhotoBrowser





open class EeScanSupportKit: RuleCell {
    public var imageView = UIImageView()
    override open func mo_model_reload() {
        let img_url = mo.model["img",""]
        imageView.img_url = img_url
        imageView.backgroundColor = UIColor.table_bg_color
    }
    
}




open class EeImagesScanCell<Kit:EeScanSupportKit>: JoTableCell{
  
    public var imgsView = RuleView<Kit>()
    
    public var browerModels:[EeImageResourceModel] = [EeImageResourceModel]()
    
    open override func addLayoutRules() {
        imgsView.space = (8,8)
        imgsView.limit_width = Swidth/3.0
        imgsView.que_num = 3
        imgsView.padding = [20]
        imgsView.item_scale = 1 //宽高比
    }
    
    open func openPhotoBrowserWithInstanceMethod(openIndex:Int,imageView:UIImageView){
        let models = browerModels
//        // 网图加载器，WebP加载器
//        let loader = JXKingfisherLoader()
//        // 数据源
//
//        let dataSource = JXRawImageDataSource(photoLoader: loader, numberOfItems: { () -> Int in
//            return models.count
//        }, placeholder: { index -> UIImage? in
//            return imageView.image
//        }, autoloadURLString: { index -> String? in
//            return models[index].secondLevelUrl
//        }) { index -> String? in
//            return models[index].thirdLevelUrl
//        }
//        // 视图代理，实现了光点型页码指示器
//        let delegate = JXDefaultPageControlDelegate()
//        // 转场动画
//        weak var ws = self
//        let trans = JXPhotoBrowserZoomTransitioning { (browser, index, view) -> UIView? in
//            let kit = ws?.imgsView.cellWithIndex(index)
//            return kit?.imageView
//        }
//        // 打开浏览器
//        JXPhotoBrowser(dataSource: dataSource, delegate: delegate, transDelegate: trans)
//            .show(pageIndex: openIndex)
        
        weak var ws = self
        let browser = JXPhotoBrowser()
               browser.numberOfItems = {
                   models.count
               }
               browser.reloadCellAtIndex = { context in
                   let url = models[context.index].secondLevelUrl.flatMap { URL(string: $0) }
                   let browserCell = context.cell as? JXPhotoBrowserImageCell
                   
                   let placeholder = imageView.image
                   // 用Kingfisher加载
                   browserCell?.imageView.kf.setImage(with: url, placeholder: placeholder, options: [], completionHandler: { _ in
                       browserCell?.setNeedsLayout()
                   })
               }
        
               browser.transitionAnimator = JXPhotoBrowserZoomAnimator(previousView: { index -> UIView? in
                   let kit = ws?.imgsView.cellWithIndex(index)
                
                   return kit?.imageView
               })
               browser.pageIndex = openIndex
               browser.show()
    }

    
    open override func didLayoutRules() {
        imgsView.setContentConstrainToHighLever()
        weak var wself = self
        imgsView.touchAction = { cell,actionID,data in
            let index = data[int:"__index",0]
            if let kit = cell as? Kit{
                wself?.openPhotoBrowserWithInstanceMethod(openIndex: index, imageView: kit.imageView)
            }
        }
        
    }
    
}



